package stackAndQueue;

import java.util.Stack;

public class TwoStackMakeQueue<T> {

  private Stack<T> stack1 = new Stack<>();
  private Stack<T> stack2 = new Stack<>();


  public void appendTail(T element) {
    stack1.push(element);
  }


  public T deleteHead() {
    //如果stack2中有数据，要先弹出
    //如果stack2中没数据，压入数据后就有数据了，然后弹出
    //如果没有数据压入，stack2也为空，那就说没有数据

    T element = null;

    //没有数据的时后，才压入数据，有数据就先弹出
    if (stack2.size() <= 0) {
      while (stack1.size() > 0) {
        stack2.push(stack1.pop());
      }
    }

    if (stack2.size() == 0) {
      throw new RuntimeException("队列中数据为空");
    }

    element = stack2.pop();
    return element;
  }

  public static void main(String args[]) {
    TwoStackMakeQueue<Integer> queue = new TwoStackMakeQueue<>();
    queue.appendTail(1);
    queue.appendTail(2);
    queue.appendTail(3);
    queue.appendTail(4);
    queue.appendTail(5);
    System.out.println(queue.deleteHead());
    System.out.println(queue.deleteHead());
    System.out.println(queue.deleteHead());
    System.out.println(queue.deleteHead());
    System.out.println(queue.deleteHead());
    System.out.println(queue.deleteHead());
  }


}
